package com.ithe.canteen.controller;


import com.ithe.canteen.common.Result;
import com.ithe.canteen.pojo.Comment;
import com.ithe.canteen.pojo.Food;
import com.ithe.canteen.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping("/queryComment")
    public Result queryComment(@RequestBody Food food) {
        String foodID = food.getFoodID();
        List<Comment> queryResult = commentService.queryComment(foodID);
        if (!queryResult.isEmpty()) {
            return Result.success(queryResult);
        }
        return Result.error("404", "暂无该菜品的评论");
    }

    @GetMapping("/manager/getCommentList")
    public Result getCommentList() {
        List<Comment> commentList = commentService.getCommentList();
        return Result.success(commentList);
    }

    @PostMapping("/manager/delComment")
    public Result deleteComment(@RequestBody Comment comment) {
        String customerID = comment.getCommentId();
        boolean delResult = commentService.deleteComment(customerID);
        if (delResult) {
            return Result.success();
        }
        return Result.error("删除评论失败");
    }

    @PostMapping("/addComment")
    public Result insertComment(@RequestBody Comment comment) {
        String orderID = comment.getCommentId();
        String commentUser = comment.getCommentUser();
        String commentContent = comment.getCommentContent();
        boolean addResult = commentService.insertComment(orderID, commentUser, commentContent);
        if (addResult) {
            return Result.success();
        }
        return Result.error("删除评论失败");
    }
}
